package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.Housekeep;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 房务信息
 */
@Component
public class HousekeepDao implements BaseDao<Housekeep> {
    @Override
    public int insert(Housekeep entity) {
        return DBHelper.executeUpdate("insert into housekeep(name,department_id) value(?,?)", entity.getName(),
            entity.getDepartment_id());
    }

    @Override
    public int update(Housekeep entity) {
        return DBHelper.executeUpdate("update housekeep set name=?,department_id=? where id=?", entity.getName(),
            entity.getDepartment_id(), entity.getId());
    }

    @Override
    public int delete(Housekeep entity) {
        return DBHelper.executeUpdate("update housekeep set is_deleted=1 where id=?", entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from housekeep where is_deleted=0");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        return DBHelper.executeQuery(
            "SELECT h.id id,h.`name` hname,department_id,d.id did,d.`name` dname from housekeep h LEFT JOIN department d ON d.id= h.department_id where 1=1 and h.is_deleted=0 limit ?,?",
            page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(Housekeep entity) {
        return DBHelper.executeQuery(
            "SELECT h.id id,h.`name` hname,department_id,d.id did,d.`name` dname from housekeep h LEFT JOIN department d ON d.id= h.department_id where h.id=?",
            entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        return Integer.valueOf(DBHelper.executeQuery("select count(*) counts from housekeep where is_deleted=0").get(0)
            .get("counts").toString());
    }

    public List<Map<String, Object>> findDepartment() {
        return DBHelper.executeQuery("select * from department where is_deleted=0");
    }
}
